package com.example.combwyaq.visualinterface.mainfragment;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.LogUtils;
import com.example.combwyaq.App;
import com.example.combwyaq.R;
import com.example.combwyaq.db.DaoManager;
import com.example.combwyaq.entity.UserEntity;
import com.example.combwyaq.mvp.contract.LREContract;
import com.example.combwyaq.mvp.di.DaggerLREComponent;
import com.example.combwyaq.mvp.di.LREModules;
import com.example.combwyaq.mvp.model.HttpCode;
import com.example.combwyaq.mvp.model.entity.BaseEntity;
import com.example.combwyaq.mvp.model.entity.TextBannerEntity;
import com.example.combwyaq.mvp.presenter.LREPresenter;
import com.example.combwyaq.mvp.viwe.BaseFragment;
import com.example.combwyaq.utils.ToastUtils;
import com.example.combwyaq.visualinterface.live.adapter.LiveAdapter;
import com.example.combwyaq.visualinterface.live.entity.LiveEntity;
import com.example.combwyaq.visualinterface.news.adapter.TextBannerAdapter;
import com.example.combwyaq.visualinterface.video.VideoNewsItemDecoration;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import java.util.HashMap;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment_Live extends BaseFragment<LREPresenter>
implements LREContract.LREView, OnRefreshLoadMoreListener {


    private SmartRefreshLayout live_smart;
    private RecyclerView live_rv;
    private View liveHead,liveHot,liveTitle;
    private Banner<TextBannerEntity.Values, TextBannerAdapter> live_head_banner;
    private LiveAdapter liveAdapter;
    private TextBannerAdapter textBannerAdapter;


    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof TextBannerEntity){
            TextBannerEntity textBannerEntity = (TextBannerEntity) entity;
            textBannerAdapter =new TextBannerAdapter(textBannerEntity.getValues());
            live_head_banner.setAdapter(textBannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer());
            LogUtils.json(textBannerEntity);
        }else if (entity instanceof LiveEntity){
            LiveEntity liveEntity= (LiveEntity) entity;
            liveAdapter.setNewInstance(liveEntity.getValues());
            LogUtils.json(liveEntity);
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity entity) {
        LiveEntity liveEntity= (LiveEntity) entity;
        if (liveEntity.getValues()==null){
            return;
        }
        liveAdapter.setNewInstance(liveEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        LiveEntity liveEntity = (LiveEntity) entity;
        if (liveEntity.getValues() == null){
            ToastUtils.showMsg("别拉了别拉了");
            return;
        }
        liveAdapter.addData(liveEntity.getValues());
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_main_fragment__live;
    }

    @Override
    public void onStart() {
        super.onStart();
        live_head_banner.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        live_head_banner.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        live_head_banner.destroy();
    }

    @Override
    public void initView() {
        live_smart = f(R.id.live_smart);
        live_rv = f(R.id.live_rv);
        liveHead = LayoutInflater.from(getContext()).inflate(R.layout.view_live_head,null);
        liveHot = LayoutInflater.from(getContext()).inflate(R.layout.view_live_hot,null);
        liveTitle = LayoutInflater.from(getContext()).inflate(R.layout.view_live_title,null);
        live_head_banner = liveHead.findViewById(R.id.live_head_banner);
        live_smart.setEnableLoadMore(true);
        live_smart.setEnableRefresh(true);
        live_smart.setRefreshHeader(new ClassicsHeader(getContext()));
        live_smart.setRefreshFooter(new ClassicsFooter(getContext()));
        live_smart.setOnRefreshLoadMoreListener(this);
        GridLayoutManager manager = new GridLayoutManager(getContext(),2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        live_rv.setLayoutManager(manager);
        live_rv.addItemDecoration(new VideoNewsItemDecoration(getContext()));
        liveAdapter = new LiveAdapter();
        liveAdapter.addHeaderView(liveHead,0);
        liveAdapter.addHeaderView(liveHot,1);
        liveAdapter.addHeaderView(liveTitle,2);
        live_rv.setAdapter(liveAdapter);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this)).build()
                .injectLiveFragment(this);

    }

    @Override
    public void initData() {
        Map<String, Object> bannerMap = new HashMap<>();
        bannerMap.put("code", HttpCode.TEXTBANNERCODE);
        Map<String, Object> liveMap = new HashMap<>();
        liveMap.put("code", HttpCode.LIVECODE);
        liveMap.put("user_id",1);
        p.requestAll(bannerMap,liveMap);

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        refreshLayout.finishLoadMore();
        Map<String, Object> liveMap = new HashMap<>();
        liveMap.put("code", HttpCode.LIVECODE);
        liveMap.put("user_id",1);
        p.requestLoad(liveMap);

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        Map<String, Object> liveMap = new HashMap<>();
        liveMap.put("code", HttpCode.LIVECODE);
        liveMap.put("user_id",1);
        p.requestRefresh(liveMap);

    }
}
